<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="layout">
			<button class="chooseFaceBtn" type="default" open-type="chooseAvatar" @chooseavatar="getUserFace">
				<image class="face" :src="face" mode="widthFix"></image>
			</button>
			<form @submit="getnickname">
				<input name="nickname" type="nickname" placeholder="请输入昵称" />
				<button form-type="submit">确认昵称</button>
			</form>			
			<button type="primary" open-type="getUserInfo" @tap="getUserInfo">微信授权登录</button>
		</view>
		<!-- #endif -->	
		
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import global from '../../common/global';
	import { ref } from 'vue';
	let session_key,openid,pageOptions;
	const face = ref('https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
	const nickname = ref('微信用户');
	
	
	const getUserInfo = () => {
		uni.getUserProfile({
			desc:'测试需要获取微信用户信息',
			success: (res) => {
				console.log(res);				
				userLogin();
			}
		})
	}
	const getUserFace = (e)=>{
		console.log(e);
		uni.uploadFile({
			url			:global.apiServer + 'uploadimg',
			filePath	:e.detail.avatarUrl,
			name		:'file',
			success: (uploadFileRes) => {				
				console.log(uploadFileRes);  //需要注意,uploadFileRes中的data是一个字符串
				uploadFileRes = JSON.parse(uploadFileRes.data);
				if(uploadFileRes.status != 'ok'){
					uni.showToast({
						title:'图片上传失败',
						icon:'none'
					})
					return ;
				}
				face.value = global.staticServer + uploadFileRes.data;
				console.log(face.value);
			}
		})
	}
	const getnickname = (e)=>{
		console.log(e);
		nickname.value = e.detail.value.nickname;
	}
	
	const userLogin = ()=>{
		uni.request({
			url: global.apiServer + 'member&m=login',
			method: 'POST',
			header:{
				'content-type':'application/x-www-form-urlencoded'
			},					
			data: {
				openid	:	openid,
				name	:	nickname.value,
				face	:	face.value
			},
			success: res => {
				console.log(res);
				res = res.data;
				uni.showToast({title:"登录成功"});
				uni.setStorageSync('SUID' , res.data.u_id + '');
				uni.setStorageSync('SRAND', res.data.u_random + '');
				uni.setStorageSync('SNAME', res.data.u_name + '');
				uni.setStorageSync('SFACE', res.data.u_face + '');				
										
				if(pageOptions.backtype == 1){
					uni.redirectTo({
						url:pageOptions.backpage
					})
				}else{
					uni.switchTab({
						url:pageOptions.backpage
					})
				}
			},
			fail: (err) => {
				console.log(err);
			},
			complete: () => {}
		});
	}

	onLoad((options) => {
		console.log(options);
		pageOptions = options;
		
		// #ifdef MP-WEIXIN
		uni.login({
			success: function(res) {
				console.log(res);
				uni.request({
					url: global.apiServer + 'member&m=codeToSession&code=' + res.code,
					method: 'GET',
					success: res => {
						console.log(res);
						session_key = res.data.session_key;
						openid 		= res.data.openid;
					}
				});
			}
		})
		// #endif	
	})
</script>

<style lang="scss">
	.layout {
		padding: 50rpx;
		.chooseFaceBtn{
			width: 150rpx;
			height: 150rpx;
			margin:0 auto;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			.face{
				width: 100%;
			}
		}
		
	}
</style>